package com.example.itcacp.controller;


import com.example.itcacp.dto.response.Result;
import com.example.itcacp.entity.Category;
import com.example.itcacp.entity.Tag;
import com.example.itcacp.service.CategoryService;
import com.example.itcacp.service.TagService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/api/cata")
public class CategoryTagController {

    @Resource
    private CategoryService categoryService;
    @Resource
    private TagService tagService;


    /**
     * 查询所有分类
     */

    @GetMapping("/selectCateAll") //接口的路径，全局唯一
    public Result selectCateAll () {
        List<Category> cateList = categoryService.selectCateAll ();
        return Result.success (cateList); //接口返回前端
    }

    /**
     * 分页查询接口: 分类
     * */
    @GetMapping("/selectCatePage")
    public Result selectCatePage (@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize,
                                  @RequestParam(required = false)String categoryName) {
        PageInfo<Category> catePageInfo =  categoryService.selectCatePage(pageNum, pageSize, categoryName);
        return Result.success (catePageInfo);
    }

    /**
     * 新增分类
     * */
    @PostMapping("/cateAdd")
    public Result cateAdd (@RequestBody Category category) {
        category.setCreated_at ( new Timestamp ( System.currentTimeMillis () ).toLocalDateTime () ); // 设置创建时间
        categoryService.save ( category ); // 保存数据到数据库
        return Result.success ();
    }

    /**
     * 修改分类
     * */
    @PutMapping("/updateCata")
    public Result updateCata (@RequestBody Category category) {
        category.setUpdated_at ( new Timestamp ( System.currentTimeMillis () ).toLocalDateTime () );
        categoryService.updateById ( category );
        return Result.success ();
    }

    /**
     * 删除分类
     * */
    @DeleteMapping("/cateDel/{categoryId}")
    public Result cateDel (@PathVariable Long categoryId) {
        categoryService.removeById ( categoryId );
        return Result.success ();
    }

    /**
     * 批量删除分类
     * */
    @DeleteMapping("/deleteCateBatch")
    public Result deleteCateBatch (@RequestBody List<Category> categoryList) {
        for (Category category : categoryList ) {  // 循环批量删除
            this.cateDel ( category.getCategoryId () );
        }
        return Result.success ();
    }

    /**
     * 查询所有标签
     */

    @GetMapping("/selectTagAll") //接口的路径，全局唯一
    public Result selectTagAll () {
        List<Tag> tagList = tagService.selectTagAll ();
        return Result.success (tagList); //接口返回前端
    }

    /**
     * 分页查询接口: 标签
     * */
    @GetMapping("/selectTagPage")
    public Result selectTagPage (@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize,
                                  @RequestParam(required = false)String tagName) {
        PageInfo<Tag> tagPageInfo =  tagService.selectTagPage(pageNum, pageSize, tagName);
        return Result.success (tagPageInfo);
    }

    /**
     * 新增标签
     * */
    @PostMapping("/tagAdd")
    public Result tagAdd (@RequestBody Tag tag) {
        tag.setCreated_at ( new Timestamp ( System.currentTimeMillis () ).toLocalDateTime () ); // 设置创建时间
        tagService.save ( tag ); // 保存数据到数据库
        return Result.success ();
    }

    /**
     * 修改标签
     * */
    @PutMapping("/updateTag")
    public Result updateTag (@RequestBody Tag tag) {
        tag.setUpdated_at ( new Timestamp ( System.currentTimeMillis () ).toLocalDateTime () );
        tagService.updateById ( tag );
        return Result.success ();
    }

    /**
     * 删除标签
     * */
    @DeleteMapping("/tagDel/{tagId}")
    public Result tagDel (@PathVariable Long tagId) {
        tagService.removeById ( tagId );
        return Result.success ();
    }

    /**
     * 批量删除标签
     * */
    @DeleteMapping("/deleteTagBatch")
    public Result deleteTagBatch (@RequestBody List<Tag> tagList) {
        for (Tag tag : tagList ) {  // 循环批量删除
            this.tagDel ( tag.getTagId () );
        }
        return Result.success ();
    }
}
